package xgenerator.ui.swing;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

import xgenerator.ui.swing.adapter.MetadataFileAdapter;

public class MetadataFileDialog extends JDialog implements UIConstants {

	/**
	 * UI组件
	 */
	private Component uiCmp;

	private JLabel filePathLabel;

	private JTextField filePathField;

	private JButton fileChooseBtn;

	private JButton loadBtn;
	
	private JButton cancelBtn;

	/**
	 * @param owner
	 * @param title
	 * @param modal
	 */
	public MetadataFileDialog(Frame owner, String title, boolean modal) {
		super(owner, title, modal);
		this.uiCmp = owner;
		initComponents();
	}

	private void initComponents() {
		this.setContentPane(buildContentPane());
		this.setTitle("加载元数据");
		this.setIconImage(IconUtils.createIconImage(APP_ICON_IMAGE));
		this.setSize(400, 130);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		// this.setResizable(false);

		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenHeight = screenSize.height;
		int screenWidth = screenSize.width;
		this.setLocation((screenWidth - getWidth()) / 2, (screenHeight - getHeight() - 30) / 2);
		this.setVisible(true);
	}
	
	/**
	 * <p>
	 * Title：
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @return
	 */
	private JPanel buildContentPane() {
		JPanel panel = new JPanel();
		// panel.setLayout(new BorderLayout());

		this.filePathLabel = new JLabel("文件路径：");
		this.filePathField = new JTextField(35);
		this.fileChooseBtn = new JButton(new AbstractAction() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				// chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				// chooser.addChoosableFileFilter(new FileNameExtensionFilter("Excel Files", "xls", "xlsx"));
				// chooser.addChoosableFileFilter(new FileNameExtensionFilter("PDM File", "pdm"));
				// chooser.addChoosableFileFilter(new FileNameExtensionFilter("XML File", "xml"));

				int returnVal = chooser.showOpenDialog(null);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					File selectFile = chooser.getSelectedFile();
					// JOptionPane.showMessageDialog(null, selectFile.getAbsoluteFile()); //for test
					filePathField.setText(selectFile.getAbsolutePath());
				}
			}
		});
		this.fileChooseBtn.setText("选择...");
		//this.fileChooseBtn.setPreferredSize(new Dimension(80, 21));
		this.loadBtn = new JButton(new MetadataFileAdapter(this));
		this.loadBtn.setText("加载");
		this.loadBtn.setActionCommand(MetadataFileAdapter.LOAD_METADATA_COMMAND);
		this.cancelBtn = new JButton(new MetadataFileAdapter(this));
		this.cancelBtn.setText("取消");
		this.cancelBtn.setActionCommand(MetadataFileAdapter.CANCEL_DIALOG_COMMAND);

		panel.add(filePathLabel);
		panel.add(filePathField);
		panel.add(fileChooseBtn);
		panel.add(loadBtn);
		panel.add(cancelBtn);

		return panel;
	}

	public JButton getCancelBtn() {
		return cancelBtn;
	}

	public void setCancelBtn(JButton cancelBtn) {
		this.cancelBtn = cancelBtn;
	}

	public JButton getFileChooseBtn() {
		return fileChooseBtn;
	}

	public void setFileChooseBtn(JButton fileChooseBtn) {
		this.fileChooseBtn = fileChooseBtn;
	}

	public JTextField getFilePathField() {
		return filePathField;
	}

	public void setFilePathField(JTextField filePathField) {
		this.filePathField = filePathField;
	}

	public JLabel getFilePathLabel() {
		return filePathLabel;
	}

	public void setFilePathLabel(JLabel filePathLabel) {
		this.filePathLabel = filePathLabel;
	}

	public JButton getLoadBtn() {
		return loadBtn;
	}

	public void setLoadBtn(JButton loadBtn) {
		this.loadBtn = loadBtn;
	}

	public Component getUiCmp() {
		return uiCmp;
	}

	public void setUiCmp(Component uiCmp) {
		this.uiCmp = uiCmp;
	}
	
}
